package com.meixiang.fragment.account;

import android.content.Context;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.lzy.okhttputils.model.HttpParams;
import com.meixiang.R;
import com.meixiang.activity.account.manager.BindPhoneActivity;
import com.meixiang.global.Config;
import com.meixiang.http.HttpCallBack;
import com.meixiang.http.HttpUtils;
import com.meixiang.main.BaseFragment;
import com.meixiang.tool.Tool;
import com.meixiang.util.KeyBoardUtils;
import com.meixiang.view.CheckEditTextEmptyButton;
import com.meixiang.view.ClearEditText;

import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 绑定手机——验证手机
 *
 * @author LZRUI
 */
public class VerifyPhoneFragment extends BaseFragment {

    private static final String PHONE_NUM = "phoneNum";

    @Bind(R.id.tv_verify_phone_hint)
    TextView tvVerifyPhoneHint;
    @Bind(R.id.edtTxt_code)
    ClearEditText edtTxtCode; // 验证码输入框
    @Bind(R.id.tv_reGet)
    TextView tvReGet; // 重新获取
    @Bind(R.id.btn_confirm)
    CheckEditTextEmptyButton btnConfirm; // 确认按钮

    private String phoneNum;

    CountDownTimer timer = new CountDownTimer(60000, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {
            tvReGet.setText((millisUntilFinished / 1000) + "(S)");
        }

        @Override
        public void onFinish() {
            tvReGet.setText("重新获取");
            tvReGet.setEnabled(true);
        }
    };

    private IBindPhoneClickListener clickListener;


    public VerifyPhoneFragment() {

    }

    public static VerifyPhoneFragment newInstance(String phoneNum) {
        VerifyPhoneFragment fragment = new VerifyPhoneFragment();
        Bundle bundle = new Bundle();
        bundle.putString(PHONE_NUM, phoneNum);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof IBindPhoneClickListener) {
            clickListener = (IBindPhoneClickListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement IVerifyPhoneFragmentToNext");
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            phoneNum = getArguments().getString(PHONE_NUM);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_verify_phone, container, false);
        ButterKnife.bind(this, rootView);
        super.onCreateView(inflater, container, savedInstanceState);
        return rootView;
    }

    @Override
    public void onPause() {
        super.onPause();
        timer.cancel();
        edtTxtCode.setText("");
        KeyBoardUtils.hideSoftInput(edtTxtCode);
    }

    @Override
    protected void initTitle() {

    }

    @Override
    protected void initView() {
        tvReGet.setEnabled(false);
        btnConfirm.setEditText(edtTxtCode);
    }

    @Override
    protected void setData() {
        timer.start();
        tvVerifyPhoneHint.setText(String.format(getString(R.string.verify_phone_num_hint), phoneNum));
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick({R.id.tv_reGet, R.id.btn_confirm})
    public void onClick(View view) {
        if (Tool.isFastDoubleClick()) return;
        switch (view.getId()) {
            // 重新获取
            case R.id.tv_reGet:
                ((BindPhoneActivity) activity).getVerifyCode(true);
                timer.start();
                break;
            // 确认
            case R.id.btn_confirm:
                verifyCode();
                break;
        }
    }

    private void verifyCode() {
        HttpUtils.post(Config.VERIFY_MOBILE_CODE, BindPhoneActivity.TAG, new HttpParams("code", edtTxtCode.getText().toString().trim()), new HttpCallBack(activity) {
            @Override
            public void onSucceed(JSONObject result, String code, String message) {
                KeyBoardUtils.hideSoftInput(edtTxtCode);
                if (clickListener != null) {
                    clickListener.onVerifyPhoneFragmentClick();
                }
            }

            @Override
            public void onError(String code, String message) {
                if (isVisible()) {
                    Toast.makeText(activity, message, Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

}
